package com.xyzwps.server.paimon.model.service

import com.xyzwps.server.paimon.model.entity.Article
import com.xyzwps.server.paimon.model.repository.ArticleRepository
import com.xyzwps.server.paimon.value.UserId
import org.springframework.stereotype.Service

@Service
class ArticleDomainService(private val articleRepository: ArticleRepository) {

    fun save(article: Article) = articleRepository.save(article)

    fun getByUidAndUserId(uid: String, userId: UserId): Article? =
        if (uid.isEmpty()) null else articleRepository.findByUidAndUserId(uid, userId)

    fun getByUid(uid: String): Article? =
        if (uid.isEmpty()) null else articleRepository.findByUid(uid)

    fun getBefore(beforeId: Long?, userId: UserId, count: Int): List<Article> {
        return if (beforeId == null) {
            articleRepository.findByUserIdOrderByIdDesc(userId, count)
        } else {
            articleRepository.findByUserIdAndIdLessThanOrderByIdDesc(userId, beforeId, count)
        }
    }
}
